package cn.edu.hbwe.book.service;

import java.sql.SQLException;

import javax.security.auth.login.LoginException;

import cn.edu.hbwe.book.dao.UserDao;
import cn.edu.hbwe.book.entity.User;
import cn.edu.hbwe.book.exception.RegisterException;
import cn.edu.hbwe.book.exception.UpdateUserInfoException;


public class UserService {
	/*
	 * 注册方法
	 */
	public void register(User user) throws RegisterException {
		//依赖dao操作数据库
		UserDao dao = new UserDao();
		try {
			dao.addUser(user);
		} catch (SQLException e) {
			// 说明注册失败
			e.printStackTrace();
			throw new RegisterException("注册失败!");
		}
		
	}
	// 登录操作
		public User login(String username, String password) throws LoginException {
			UserDao dao = new UserDao();
			try {
				//根据登录时表单输入的用户名和密码，查找用户
				User user = dao.findUserByUsernameAndPassword(username, password);
				return user;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new LoginException("用户名或密码错误");
			}
		}
		// 更新用户信息
		public void updateUserInfo(int id, String password, String gender, String telephone) throws UpdateUserInfoException {
			UserDao dao = new UserDao();
			try {
				dao.updateUserInfo(id, password, gender, telephone) ;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new UpdateUserInfoException("用户信息更新错误");
			}
		}
}